from utils.log_util import logger
from utils.mysql_util import db


# 获取短信验证码
def get_code(mobile):
    sql = "select code from users_verifycode where mobile = '%s' order by id desc limit 1" % mobile
    result = db.select_db_one(sql)
    logger.info(f'sql语句执行结果：{result}')
    return result['code']


# 删除用户
def delete_user(mobile):
    sql = "DELETE FROM users_userprofile WHERE username = '%s';" % mobile
    result = db.execute_db(sql)
    logger.info(f'sql语句执行结果{result}')


# 删除用户验证码
def delete_code(mobile):
    sql = "DELETE FROM users_userprofile WHERE username = '%s'" % mobile
    result = db.execute_db(sql)
    logger.info(f'sql语句执行结果{result}')


# 查询商品编号
def search_id(mobile):
    sql = "SELECT id FROM users_userprofile WHERE username = '%s'" % mobile
    user_id = db.select_db_one(sql)['id']
    sql1 = "SELECT goods_id FROM trade_shoppingcart WHERE user_id = '%s'" % user_id
    goods_id = db.select_db_one(sql1)
    logger.info(f'sql语句查询商品编号为：{goods_id}')
    return goods_id['goods_id']


# 删除已添加的商品
def delete_goods(mobile):
    sql = "SELECT id FROM users_userprofile WHERE username = '%s'" % mobile
    user_id = db.select_db_one(sql)['id']
    sql = "DELETE FROM trade_shoppingcart WHERE user_id = '%s';" % user_id
    result = db.execute_db(sql)
    logger.info(f'sql语句删除商品：{result}')
